<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="背景在iOS端由于文件系统的封闭性，文件的上传变得十分麻烦，一个比较好的解决方案是通过局域网WiFi来传输文件并存储到沙盒中。
简介SGWiFiUpload是一个基于CocoaHTTPServer的WiFi上传框架。CocoaHTTPServer是一个可运行于iOS和OS X上的轻量级服务端框架，可以处理GET和POST请求，通过对代码的初步改造，实现了iOS端的WiFi文件上传与上传状态监听。">
<meta property="og:type" content="article">
<meta property="og:title" content="WiFi文件上传框架SGWiFiUpload">
<meta property="og:url" content="http://yoursite.com/2016/10/17/WiFi文件上传框架SGWiFiUpload/index.html">
<meta property="og:site_name" content="Soulghost's Blog">
<meta property="og:description" content="背景在iOS端由于文件系统的封闭性，文件的上传变得十分麻烦，一个比较好的解决方案是通过局域网WiFi来传输文件并存储到沙盒中。
简介SGWiFiUpload是一个基于CocoaHTTPServer的WiFi上传框架。CocoaHTTPServer是一个可运行于iOS和OS X上的轻量级服务端框架，可以处理GET和POST请求，通过对代码的初步改造，实现了iOS端的WiFi文件上传与上传状态监听。">
<meta property="og:updated_time" content="2016-10-19T13:27:03.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="WiFi文件上传框架SGWiFiUpload">
<meta name="twitter:description" content="背景在iOS端由于文件系统的封闭性，文件的上传变得十分麻烦，一个比较好的解决方案是通过局域网WiFi来传输文件并存储到沙盒中。
简介SGWiFiUpload是一个基于CocoaHTTPServer的WiFi上传框架。CocoaHTTPServer是一个可运行于iOS和OS X上的轻量级服务端框架，可以处理GET和POST请求，通过对代码的初步改造，实现了iOS端的WiFi文件上传与上传状态监听。">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: 'Author'
    }
  };
</script>




  <link rel="canonical" href="http://yoursite.com/2016/10/17/WiFi文件上传框架SGWiFiUpload/"/>


  <title> WiFi文件上传框架SGWiFiUpload | Soulghost's Blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?1dcddcee252f3e39b5d9411ff6e6eab2";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Soulghost's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            About
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                WiFi文件上传框架SGWiFiUpload
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2016-10-17T21:56:53+08:00" content="2016-10-17">
              2016-10-17
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/iOS/" itemprop="url" rel="index">
                    <span itemprop="name">iOS</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/10/17/WiFi文件上传框架SGWiFiUpload/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/10/17/WiFi文件上传框架SGWiFiUpload/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h3><p>在iOS端由于文件系统的封闭性，文件的上传变得十分麻烦，一个比较好的解决方案是通过局域网WiFi来传输文件并存储到沙盒中。</p>
<h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p><a href="https://github.com/Soulghost/SGWiFiUpload" target="_blank" rel="external">SGWiFiUpload</a>是一个基于<a href="https://github.com/robbiehanson/CocoaHTTPServer" target="_blank" rel="external">CocoaHTTPServer</a>的WiFi上传框架。CocoaHTTPServer是一个可运行于iOS和OS X上的轻量级服务端框架，可以处理GET和POST请求，通过对代码的初步改造，实现了iOS端的WiFi文件上传与上传状态监听。</p>
<a id="more"></a>
<h3 id="下载与使用"><a href="#下载与使用" class="headerlink" title="下载与使用"></a>下载与使用</h3><p>目前已经做成了易用的框架，上传到了GitHub，<a href="https://github.com/Soulghost/SGWiFiUpload" target="_blank" rel="external">点击这里进入</a>，欢迎Star！</p>
<h3 id="请求的处理"><a href="#请求的处理" class="headerlink" title="请求的处理"></a>请求的处理</h3><p>CocoaHTTPServer通过HTTPConnection这一接口实现类来回调网络请求的各个状态，包括对请求头、响应体的解析等。为了实现文件上传，需要自定义一个继承HTTPConnection的类，这里命名为<code>SGHTTPConnection</code>，与文件上传有关的几个方法如下。</p>
<h4 id="解析文件上传的请求头"><a href="#解析文件上传的请求头" class="headerlink" title="解析文件上传的请求头"></a>解析文件上传的请求头</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line">- (void)processStartOfPartWithHeader:(MultipartMessageHeader*) header &#123;</div><div class="line"></div><div class="line">	// in this sample, we are not interested in parts, other then file parts.</div><div class="line">	// check content disposition to find out filename</div><div class="line"></div><div class="line">    MultipartMessageHeaderField* disposition = [header.fields objectForKey:@&quot;Content-Disposition&quot;];</div><div class="line">	NSString* filename = [[disposition.params objectForKey:@&quot;filename&quot;] lastPathComponent];</div><div class="line"></div><div class="line">    if ( (nil == filename) || [filename isEqualToString: @&quot;&quot;] ) &#123;</div><div class="line">        // it&apos;s either not a file part, or</div><div class="line">		// an empty form sent. we won&apos;t handle it.</div><div class="line">		return;</div><div class="line">	&#125;</div><div class="line">    // 这里用于发出文件开始上传的通知</div><div class="line">    dispatch_async(dispatch_get_main_queue(), ^&#123;</div><div class="line">        [[NSNotificationCenter defaultCenter] postNotificationName:SGFileUploadDidStartNotification object:@&#123;@&quot;fileName&quot; : filename ?: @&quot;File&quot;&#125;];</div><div class="line">    &#125;);</div><div class="line">    // 这里用于设置文件的保存路径，先预存一个空文件，然后进行追加写内容</div><div class="line">    NSString *uploadDirPath = [SGWiFiUploadManager sharedManager].savePath;</div><div class="line">	BOOL isDir = YES;</div><div class="line">	if (![[NSFileManager defaultManager]fileExistsAtPath:uploadDirPath isDirectory:&amp;isDir ]) &#123;</div><div class="line">		[[NSFileManager defaultManager]createDirectoryAtPath:uploadDirPath withIntermediateDirectories:YES attributes:nil error:nil];</div><div class="line">	&#125;</div><div class="line"></div><div class="line">    NSString* filePath = [uploadDirPath stringByAppendingPathComponent: filename];</div><div class="line">    if( [[NSFileManager defaultManager] fileExistsAtPath:filePath] ) &#123;</div><div class="line">        storeFile = nil;</div><div class="line">    &#125;</div><div class="line">    else &#123;</div><div class="line">		HTTPLogVerbose(@&quot;Saving file to %@&quot;, filePath);</div><div class="line">		if(![[NSFileManager defaultManager] createDirectoryAtPath:uploadDirPath withIntermediateDirectories:true attributes:nil error:nil]) &#123;</div><div class="line">			HTTPLogError(@&quot;Could not create directory at path: %@&quot;, filePath);</div><div class="line">		&#125;</div><div class="line">		if(![[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]) &#123;</div><div class="line">			HTTPLogError(@&quot;Could not create file at path: %@&quot;, filePath);</div><div class="line">		&#125;</div><div class="line">		storeFile = [NSFileHandle fileHandleForWritingAtPath:filePath];</div><div class="line">		[uploadedFiles addObject: [NSString stringWithFormat:@&quot;/upload/%@&quot;, filename]];</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>其中有中文注释的两处是比较重要的地方，这里根据请求头发出了文件开始上传的通知，并且往要存放的路径写一个空文件，以便后续追加内容。</p>
<h4 id="上传过程中的处理"><a href="#上传过程中的处理" class="headerlink" title="上传过程中的处理"></a>上传过程中的处理</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">- (void) processContent:(NSData*) data WithHeader:(MultipartMessageHeader*) header</div><div class="line">&#123;</div><div class="line">	// here we just write the output from parser to the file.</div><div class="line">    // 由于除去文件内容外，还有HTML内容和空文件通过此方法处理，因此需要过滤掉HTML和空文件内容</div><div class="line">    if (!header.fields[@&quot;Content-Disposition&quot;]) &#123;</div><div class="line">        return;</div><div class="line">    &#125; else &#123;</div><div class="line">        MultipartMessageHeaderField *field = header.fields[@&quot;Content-Disposition&quot;];</div><div class="line">        NSString *fileName = field.params[@&quot;filename&quot;];</div><div class="line">        if (fileName.length == 0) return;</div><div class="line">    &#125;</div><div class="line">    self.currentLength += data.length;</div><div class="line">    CGFloat progress;</div><div class="line">    if (self.contentLength == 0) &#123;</div><div class="line">        progress = 1.0f;</div><div class="line">    &#125; else &#123;</div><div class="line">        progress = (CGFloat)self.currentLength / self.contentLength;</div><div class="line">    &#125;</div><div class="line">    dispatch_async(dispatch_get_main_queue(), ^&#123;</div><div class="line">       [[NSNotificationCenter defaultCenter] postNotificationName:SGFileUploadProgressNotification object:@&#123;@&quot;progress&quot; : @(progress)&#125;];</div><div class="line">    &#125;);</div><div class="line">	if (storeFile) &#123;</div><div class="line">		[storeFile writeData:data];</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这里除了拼接文件内容以外，还发出了上传进度的通知，当前方法中只能拿到这一段文件的长度，总长度需要通过下面的方法拿到。</p>
<h4 id="获取文件大小"><a href="#获取文件大小" class="headerlink" title="获取文件大小"></a>获取文件大小</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">- (void)prepareForBodyWithSize:(UInt64)contentLength</div><div class="line">&#123;</div><div class="line">	HTTPLogTrace();</div><div class="line">	// 设置文件总大小，并初始化当前已经传输的文件大小。</div><div class="line">    self.contentLength = contentLength;</div><div class="line">    self.currentLength = 0;</div><div class="line">	// set up mime parser</div><div class="line">    NSString* boundary = [request headerField:@&quot;boundary&quot;];</div><div class="line">    parser = [[MultipartFormDataParser alloc] initWithBoundary:boundary formEncoding:NSUTF8StringEncoding];</div><div class="line">    parser.delegate = self;</div><div class="line"></div><div class="line">	uploadedFiles = [[NSMutableArray alloc] init];</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h4 id="处理传输完毕"><a href="#处理传输完毕" class="headerlink" title="处理传输完毕"></a>处理传输完毕</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">- (void) processEndOfPartWithHeader:(MultipartMessageHeader*) header</div><div class="line">&#123;</div><div class="line">    // as the file part is over, we close the file.</div><div class="line">    // 由于除去文件内容外，还有HTML内容和空文件通过此方法处理，因此需要过滤掉HTML和空文件内容</div><div class="line">    if (!header.fields[@&quot;Content-Disposition&quot;]) &#123;</div><div class="line">        return;</div><div class="line">    &#125; else &#123;</div><div class="line">        MultipartMessageHeaderField *field = header.fields[@&quot;Content-Disposition&quot;];</div><div class="line">        NSString *fileName = field.params[@&quot;filename&quot;];</div><div class="line">        if (fileName.length == 0) return;</div><div class="line">    &#125;</div><div class="line">	[storeFile closeFile];</div><div class="line">	storeFile = nil;</div><div class="line">    dispatch_async(dispatch_get_main_queue(), ^&#123;</div><div class="line">        [[NSNotificationCenter defaultCenter] postNotificationName:SGFileUploadDidEndNotification object:nil];</div><div class="line">    &#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这里关闭了文件管道，并且发出了文件上传完毕的通知。</p>
<h3 id="开启Server"><a href="#开启Server" class="headerlink" title="开启Server"></a>开启Server</h3><p>CocoaHTTPServer默认的Web根目录为MainBundle，他会在目录下寻找index.html，文件上传的请求地址为upload.html，当以POST方式请求upload.html时，请求会被Server拦截，并且交由HTTPConnection处理。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">- (BOOL)startHTTPServerAtPort:(UInt16)port &#123;</div><div class="line">    HTTPServer *server = [HTTPServer new];</div><div class="line">    server.port = port;</div><div class="line">    self.httpServer = server;</div><div class="line">    [self.httpServer setDocumentRoot:self.webPath];</div><div class="line">    [self.httpServer setConnectionClass:[SGHTTPConnection class]];</div><div class="line">    NSError *error = nil;</div><div class="line">    [self.httpServer start:&amp;error];</div><div class="line">    return error == nil;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="在HTML中发送POST请求上传文件"><a href="#在HTML中发送POST请求上传文件" class="headerlink" title="在HTML中发送POST请求上传文件"></a>在HTML中发送POST请求上传文件</h3><p>在CocoaHTTPServer给出的样例中有用于文件上传的index.html，要实现文件上传，只需要一个POST方法的form表单，action为upload.html，每一个文件使用一个input标签，type为file即可，这里为了美观对input标签进行了自定义。<br>下面的代码演示了能同时上传3个文件的index.html代码。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">\</span>"<span class="attr">content-type</span>\" <span class="attr">content</span>=<span class="string">\</span>"<span class="attr">text</span>/<span class="attr">html</span>; <span class="attr">charset</span>=<span class="string">UTF-8\</span>"&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="undefined"></span></div><div class="line">    body &#123;</div><div class="line">        margin: 0px;</div><div class="line">        padding: 0px;</div><div class="line">        font-size: 12px;</div><div class="line">        background-color: rgb(244,244,244);</div><div class="line">        text-align: center;</div><div class="line">    &#125;</div><div class="line">    #container &#123;</div><div class="line">        margin: auto;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    #form &#123;</div><div class="line">        margin-top: 60px;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    .upload &#123;</div><div class="line">        margin-top: 2px;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    #submit input &#123;</div><div class="line">        background-color: #ea4c88;</div><div class="line">        color: #eee;</div><div class="line">        font-weight: bold;</div><div class="line">        margin-top: 10px;</div><div class="line">        text-align: center;</div><div class="line">        font-size: 16px;</div><div class="line">        border: none;</div><div class="line">        width: 120px;</div><div class="line">        height: 36px;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    #submit input:hover &#123;</div><div class="line">        background-color: #d44179;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    #submit input:active &#123;</div><div class="line">        background-color: #a23351;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    .uploadField &#123;</div><div class="line">        margin-top: 2px;</div><div class="line">        width: 200px;</div><div class="line">        height: 22px;</div><div class="line">        font-size: 12px;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    .uploadButton &#123;</div><div class="line">        background-color: #ea4c88;</div><div class="line">        color: #eee;</div><div class="line">        font-weight: bold;</div><div class="line">        text-align: center;</div><div class="line">        font-size: 15px;</div><div class="line">        border: none;</div><div class="line">        width: 80px;</div><div class="line">        height: 26px;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    .uploadButton:hover &#123;</div><div class="line">        background-color: #d44179;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    .uploadButton:active &#123;</div><div class="line">        background-color: #a23351;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="tag">&lt;/<span class="name">style</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"container"</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"form"</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">h2</span>&gt;</span>WiFi File Upload<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">form</span> <span class="attr">name</span>=<span class="string">"form"</span> <span class="attr">action</span>=<span class="string">"upload.html"</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">enctype</span>=<span class="string">"multipart/form-data"</span> <span class="attr">accept-charset</span>=<span class="string">"utf-8"</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"upload"</span>&gt;</span></div><div class="line">                        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">name</span>=<span class="string">"upload1"</span> <span class="attr">id</span>=<span class="string">"upload1"</span> <span class="attr">style</span>=<span class="string">"display:none"</span> <span class="attr">onChange</span>=<span class="string">"document.form.path1.value=this.value"</span>&gt;</span></div><div class="line">                            <span class="tag">&lt;<span class="name">input</span> <span class="attr">class</span>=<span class="string">"uploadField"</span> <span class="attr">name</span>=<span class="string">"path1"</span> <span class="attr">readonly</span>&gt;</span></div><div class="line">                                <span class="tag">&lt;<span class="name">input</span> <span class="attr">class</span>=<span class="string">"uploadButton"</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"Open"</span> <span class="attr">onclick</span>=<span class="string">"document.form.upload1.click()"</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"upload"</span>&gt;</span></div><div class="line">                        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">name</span>=<span class="string">"upload2"</span> <span class="attr">id</span>=<span class="string">"upload2"</span> <span class="attr">style</span>=<span class="string">"display:none"</span> <span class="attr">onChange</span>=<span class="string">"document.form.path2.value=this.value"</span>&gt;</span></div><div class="line">                            <span class="tag">&lt;<span class="name">input</span> <span class="attr">class</span>=<span class="string">"uploadField"</span> <span class="attr">name</span>=<span class="string">"path2"</span> <span class="attr">readonly</span>&gt;</span></div><div class="line">                                <span class="tag">&lt;<span class="name">input</span> <span class="attr">class</span>=<span class="string">"uploadButton"</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"Open"</span> <span class="attr">onclick</span>=<span class="string">"document.form.upload2.click()"</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"upload"</span>&gt;</span></div><div class="line">                        <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"file"</span> <span class="attr">name</span>=<span class="string">"upload3"</span> <span class="attr">id</span>=<span class="string">"upload3"</span> <span class="attr">style</span>=<span class="string">"display:none"</span> <span class="attr">onChange</span>=<span class="string">"document.form.path3.value=this.value"</span>&gt;</span></div><div class="line">                            <span class="tag">&lt;<span class="name">input</span> <span class="attr">class</span>=<span class="string">"uploadField"</span> <span class="attr">name</span>=<span class="string">"path3"</span> <span class="attr">readonly</span>&gt;</span></div><div class="line">                                <span class="tag">&lt;<span class="name">input</span> <span class="attr">class</span>=<span class="string">"uploadButton"</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"Open"</span> <span class="attr">onclick</span>=<span class="string">"document.form.upload3.click()"</span>&gt;</span></div><div class="line">                                    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"submit"</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Submit"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">                <span class="tag">&lt;/<span class="name">form</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<p>表单提交后，会进入upload.html页面，该页面用于说明上传完毕，下面的代码实现了3秒后的重定向返回。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">\</span>"<span class="attr">content-type</span>\" <span class="attr">content</span>=<span class="string">\</span>"<span class="attr">text</span>/<span class="attr">html</span>; <span class="attr">charset</span>=<span class="string">UTF-8\</span>"&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">refresh</span> <span class="attr">content</span>=<span class="string">"3;url=index.html"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line">	<span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">		<span class="tag">&lt;<span class="name">h3</span>&gt;</span>Upload Succeeded!<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">p</span>&gt;</span>The Page will be back in 3 seconds<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line">	<span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/19/基于AOP的iOS用户操作引导框架设计/" rel="prev" title="基于AOP的iOS用户操作引导框架设计">
                基于AOP的iOS用户操作引导框架设计 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/10/17/WiFi文件上传框架SGWiFiUpload/"
           data-title="WiFi文件上传框架SGWiFiUpload" data-url="http://yoursite.com/2016/10/17/WiFi文件上传框架SGWiFiUpload/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="Soulghost(小修修)" />
          <p class="site-author-name" itemprop="name">Soulghost(小修修)</p>
          <p class="site-description motion-element" itemprop="description">Not matter what happened, I'll be here forerver more.</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">2</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">categories</span>
              
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="https://jyoung233.github.io" title="JYoung" target="_blank">JYoung</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#背景"><span class="nav-number">1.</span> <span class="nav-text">背景</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#简介"><span class="nav-number">2.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#下载与使用"><span class="nav-number">3.</span> <span class="nav-text">下载与使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#请求的处理"><span class="nav-number">4.</span> <span class="nav-text">请求的处理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#解析文件上传的请求头"><span class="nav-number">4.1.</span> <span class="nav-text">解析文件上传的请求头</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#上传过程中的处理"><span class="nav-number">4.2.</span> <span class="nav-text">上传过程中的处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取文件大小"><span class="nav-number">4.3.</span> <span class="nav-text">获取文件大小</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#处理传输完毕"><span class="nav-number">4.4.</span> <span class="nav-text">处理传输完毕</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#开启Server"><span class="nav-number">5.</span> <span class="nav-text">开启Server</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在HTML中发送POST请求上传文件"><span class="nav-number">6.</span> <span class="nav-text">在HTML中发送POST请求上传文件</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Soulghost(小修修)</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"soulghost"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  


</body>
</html>
